;(function () {
  Array.prototype.findArr = function() {
    var res = [this[0]];
    for (var i = 1; i < this.length; i++) {
      var repeat = false;
      for (var j = 0; j < res.length; j++) {
        if (this[i].projectId == res[j].projectId) {
          repeat = true;
        }
      }
      if (!repeat) {
        res.push(this[i]);
      }
    }
    return res;
  };

  new Vue({
    el: '#price_edit',
    data: {
      project: [],
      building: [],
      unitType: [],
      BuList: [],
      tyAct: [],
      projectId: [],
      buildId: [],
      batchType: false,
      params: [],
      buType: [{code: 'D', memo: '(元/m²/天)'},{code: 'M', memo: '(元/m²/月)'}],
      wpType: [{code: 'D', memo: '(元/个/天)'},{code: 'M', memo: '(元/个/月)'}]
    },
    filters: {
      formatBusinessType:function(item){
        return hlCodeConvert.businessType(item)
      }
    },
    methods: {
      // 获取项目数据
      getProjectData:function(){
        var self = this;
        const pid = hlUrlUtil.GetQueryString('pid')

        hlhttp.get({
          url:'user/projects',
          success:function(data){
            data = data.findArr();
            self.project = hlArrayUtil.replaceData(data, [{key: 'projectName', rKey: 'name'}, {key: 'projectId', rKey: 'code'}]);
            self.projectId = pid ? [pid] : []
          }
        })
      },
      // 获取项目楼宇列表数据
      getBuildingData:function(item){
        var self = this;
        this.projectId = [item[0].code];
        this.building = [];
        this.BuList = [];
        this.buildId = [];
        hlhttp.get({
          url:'project/buildings',
          data: {project_id: item[0].code},
          success:function(data){
            self.building = hlArrayUtil.replaceData(data, [{key: 'buildingId', rKey: 'code'}]);
          }
        })
      },
			// 如果初次有项目id则调用
			ifProjectIdGetBuildingData:function(){
			  hlhttp.get({
			    url:'project/buildings',
			    data: {project_id: hlUrlUtil.GetQueryString('pid')},
			    success:(data) => {
			      this.building = hlArrayUtil.replaceData(data, [{key: 'buildingId', rKey: 'code'}]);
						this.buildId = [hlUrlUtil.GetQueryString('bid')];
			    }
			  })
			},
      //获取楼宇id
      getbuildId: function (item) {
        this.buildId = [item[0].code];
        this.BuList = [];
      },
      //获取单元类型
      getType: function (arr) {
        this.tyAct = [arr[0].code];
        this.BuList = [];
        this.params = [];
      },
      //设置取消
      setCel: function (i, s) {
        if (s == '1') {
          this.BuList[i].flag = true
        }
        if (s == '0') {
          this.BuList[i].flag = false;
          this.getBuList()
        }
      },
      //批量设置
      batchSet: function (s) {
        if (!this.BuList) return;
        if (!this.projectId && !this.buildId && !this.tyAct.length) return;
        if (s == '0') {
          this.batchType = true;
          for (var i = 0; i < this.BuList.length; i++) {
            this.BuList[i].flag = true;
          }
        }
        if (s == '1') {
          this.batchType = false;
          this.getBuList()
        }
      },
      //日月类型
      dmType: function (i,type) {
        this.BuList[i].basePriceType = type;
      },
      //验证数字类型
      valdataNumber: function (num, i) {
        if (!Number(num)) {
          hl.toast('请填写数字')
          this.BuList[i].basePrice = null;
        }
      },
      //单独设置
      setPrice: function (id, price, i, type) {
        var self = this;
        if (!price) return;
        if (this.tyAct[0] == 'BU') {
          var res1 = [{unit_id: id, base_price: price, base_price_type: type}];
          hlhttp.post({
            url:'units/base_price',
            data: {
              unit_base_price: JSON.stringify(res1)
            },
            success:function(data){
              hl.toast('设置单价成功')
              self.BuList[i].flag = false
            }
          })
        }
        if (this.tyAct[0] == 'WP') {
          var res2 = [{position_id: id, base_price: price,base_price_type: type}]
          hlhttp.post({
            url:'positions/base_price',
            data: {
              position_base_price: JSON.stringify(res2)
            },
            success:function(data){
              hl.toast('设置单价成功')
              self.BuList[i].flag = false
            }
          })
        }
      },
      //向下赋值
      downVal: function (j) {
        if (!this.BuList[j].basePrice) return;
        for (var i = 0; i < this.BuList.length; i++) {
          if (i > j) {
            this.BuList[i].basePrice = this.BuList[j].basePrice
            this.BuList[i].basePriceType = this.BuList[j].basePriceType
          }
        }
      },
      //重置
      resetButton: function () {
        this.projectId = [];
        this.buildId = [];
        this.batchType = false;
        this.tyAct = [];
        this.building = [];
      },
      //提交
      buttonSubmit: function () {
        var self = this, res1 = [], res2 = [];
        if (this.tyAct[0] == 'BU') {
          for (var i = 0; i < this.BuList.length; i++) {
            if (this.BuList[i].basePrice) {
              res1.push({unit_id: this.BuList[i].unitId, base_price: this.BuList[i].basePrice,base_price_type: this.BuList[i].basePriceType})
            }
          }
          hlhttp.post({
            url:'units/base_price',
            data: {
              unit_base_price: JSON.stringify(res1)
            },
            success:function(data){
              hl.toast('设置单价成功')
              self.getBuList()
              self.batchType = false;
            }
          })
        }
        if (this.tyAct[0] == 'WP') {
          for (var i = 0; i < this.BuList.length; i++) {
            if (this.BuList[i].basePrice) {
              res2.push({position_id: this.BuList[i].positionId, base_price: this.BuList[i].basePrice, base_price_type: this.BuList[i].basePriceType})
            }
          }
          hlhttp.post({
            url:'positions/base_price',
            data: {
              position_base_price: JSON.stringify(res2)
            },
            success:function(data){
              hl.toast('设置单价成功')
              self.getBuList();
              self.batchType = false;
            }
          })
        }
      },
      // 获取单元类型列表
      getUnitType:function(container){
        var self = this;
        hlhttp.get({
          url:'unit_types',
          success:function(data){
            self.unitType = hlArrayUtil.replaceData(data, [{key: 'memo', rKey: 'name'}]);
          }
        })
      },
      //获取楼宇普通单元列表
      getBuList: function () {
        console.log(this.projectId);
        console.log(this.buildId);

        if (!this.projectId.length) {
          hl.toast('请选择项目')
          return
        }
        if (!this.buildId.length) {
          hl.toast('请选择楼宇')
          return
        }
        if (!this.tyAct.length) {
          hl.toast('请选择单元类型')
          return
        }

        console.log(this.tyAct[0]);

        var self = this;
        if (this.tyAct[0] == 'BU') {
          hlhttp.get({
            url:'buildings/bu_units',
            data: {project_id: self.projectId[0], building_id: self.buildId[0]},
            success:function(data){
              for (var i = 0; i < data.length; i++) {
                data[i].flag = false;
                data[i].selected = false;
                if (!data[i].basePrice) {
                  data[i].basePrice = null
                }
                if (!data[i].basePriceType) {
                  data[i].basePriceType = 'D'
                }
              }
              self.BuList = data
            }
          })
        }

        if (this.tyAct[0] == 'WP') {
          hlhttp.get({
            url:'buildings/wps',
            data: {project_id: self.projectId[0], building_id: self.buildId[0]},
            success:function(data){
              for (var i = 0; i < data.length; i++) {
                data[i].flag = false;
                data[i].selected = false;
                if (!data[i].basePrice) {
                  data[i].basePrice = null
                }
              }
              self.BuList = data
            }
          })
        }
      },
      flagSelect: function (j) {
        for (var i = 0; i < this.BuList.length; i++) {
          if (i != j) {
            this.BuList[i].selected = false;
          }
        }
        this.BuList[j].selected = !this.BuList[j].selected;
      }
    },
    mounted: function () {
      this.getProjectData()
      this.getUnitType()
      hlSelectUi()
      if (hlUrlUtil.GetQueryString('pid')) {
        this.ifProjectIdGetBuildingData()
      }
      var self = this;
      document.addEventListener('click', function (e) {
        if (!self.$el.contains(e.target)) {
          for (var i = 0; i < self.BuList.length; i++) {
            self.BuList[i].selected = false;
          }
        }
      })
    }
  })
} ())
